package com.yangtzeu.jwc.dao.impl;

import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.http.Header;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.yangtzeu.jwc.app.MyApplication;
import com.yangtzeu.jwc.businessservice.BusinessHttpCallBack;
import com.yangtzeu.jwc.utils.ACache;
import com.yangtzeu.jwc.utils.G;
import com.zjsx.lib.utils.L;

public class BasicDaoImpl {
    protected MyApplication app;
    protected static String viewstate;
    protected static String eventvalidation;
    protected Map<String, String> addCustomParams;
    protected Map<String, String> paramsMap;
    protected HttpDaoImpl httpDao;
    protected ACache aCache;

    public BasicDaoImpl() {
        app = MyApplication.getInstance();
        paramsMap = new LinkedHashMap<String, String>();
        httpDao = new HttpDaoImpl(app);
        aCache = ACache.get(app);
    }

    public void setValidation(final String url, final BusinessHttpCallBack callBack) {
        doHttpRequest(url, new BusinessHttpCallBack() {

            @Override
            public void onFailure(byte[] responseBody, String failuremsg, boolean isNoticeDialog) {
                //获取验证失败
                callBack.onFailure(responseBody, failuremsg, isNoticeDialog);
            }

            @Override
            public void onSuccess(byte[] responseBody, String successMsg, boolean isNoticeDialog, Object obj) {
                //获取验证成功
                callBack.onSuccess(responseBody, successMsg, isNoticeDialog, obj);
            }

            @Override
            public void onProgress(int bytesWritten, int totalSize) {
                callBack.onProgress(bytesWritten, totalSize);
            }
        });

    }

    /**
     * 设置表单验证
     *
     * @param doc
     */
    protected void setValidation(Document doc) {
        viewstate = doc.select("#" + G.VIEWSTATE).val();
        eventvalidation = doc.select("#" + G.EVENTVALIDATION).val();
    }

    /**
     * 设置基本参数
     */
    protected BasicDaoImpl addCustomParams(String key, String value) {
        paramsMap.put(key, value);
        return this;
    }

    protected void removeParams(String key) {
        paramsMap.remove(key);
    }

    protected void clearParams() {
        paramsMap.clear();
    }

    protected BasicDaoImpl addValidationParams() {
        paramsMap.put(G.VIEWSTATE, viewstate);
        paramsMap.put(G.EVENTVALIDATION, eventvalidation);
        return this;
    }

    protected Map<String, String> getParamsMap() {
        return paramsMap;
    }

    protected void doHttpRequest(String path, final BusinessHttpCallBack callBack) {
        httpDao.doHttpRequest(app, path, new AsyncHttpResponseHandler() {

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                callBack.onFailure(arg2, "请检查网络是否可用！或确保教务处能够访问！", false);
            }

            @Override
            public void onProgress(int bytesWritten, int totalSize) {
                callBack.onProgress(bytesWritten, totalSize);
            }

            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                String html = null;
                try {
                    html = new String(arg2, "gb2312");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                L.d("数据："+html);
                Document doc = Jsoup.parse(html);
                setValidation(doc);
                callBack.onSuccess(arg2, null, false, doc);
            }

        });
    }

    protected void doHttpRequest(String path, Map<String, String> paramsMap2, final BusinessHttpCallBack callBack) {
        httpDao.doHttpRequest(app, path, paramsMap2, new AsyncHttpResponseHandler() {
            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                callBack.onFailure(arg2, "请检查网络是否可用！或确保教务处能够访问！", false);
                ;
            }

            @Override
            public void onProgress(int bytesWritten, int totalSize) {
                callBack.onProgress(bytesWritten, totalSize);
            }

            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                String html = null;
                try {
                    html = new String(arg2, "gb2312");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                L.d("数据："+html);
                Document doc = Jsoup.parse(html);
                setValidation(doc);
                callBack.onSuccess(arg2, null, false, doc);
            }

        });
    }
}
